﻿using System;
using System.Text;

namespace LightINI
{
    partial class Ini
    {
        /// <summary>
        /// 配置项
        /// </summary>
        public class Item
        {
            /// <summary>
            /// 键名
            /// </summary>
            public string Key { get; set; }
            /// <summary>
            /// 值
            /// </summary>
            public string Value { get; set; }
            /// <summary>
            /// 配置项前的行(空行与注释行)
            /// </summary>
            public string[] HeadRows { get; set; }
            /// <summary>
            /// 配置项行末尾处的注释
            /// </summary>
            public string Comment { get; set; }

            public override bool Equals(object obj)
            {
                return Key != null && Key.Equals(obj.ToString(), StringComparison.OrdinalIgnoreCase);
            }

            public override int GetHashCode()
            {
                return Key.GetHashCode();
            }

            public override string ToString()
            {
                var buffer = new StringBuilder();

                if (HeadRows != null)
                {
                    foreach (var row in HeadRows)
                    {
                        buffer.AppendLine(row);
                    }
                }

                buffer.AppendLine(string.Format("{0}={1} {2}", Key, Value, Comment).Trim());

                return buffer.ToString();
            }
        }
    }
}
